package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
@Service
@Slf4j
public class SetmealServiceImp implements SetmealService {
    @Resource
    SetmealMapper setmealMapper;

    @Resource
    SetmealDishMapper setmealDishMapper;
    @Override
    @Transactional
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);

        setmealMapper.insert(setmeal);
        Long id = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(id!=null && setmealDishes!=null && !setmealDishes.isEmpty() ){
            for(SetmealDish setmealDish :setmealDishes){
                setmealDish.setSetmealId(id);
            }
            setmealDishMapper.insertBatch(setmealDishes);
        }

    }

    @Override
    public PageResult getPage(SetmealPageQueryDTO page) {
        PageHelper.startPage(page.getPage(), page.getPageSize());
        Page<SetmealVO> result = setmealMapper.getPage(page);

        return new PageResult(result.getTotal(),result.getResult());
    }

    @Override
    public void update(SetmealDTO setmealDTO) {
        log.info("{}", setmealDTO);
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if(setmealDishes!=null && !setmealDishes.isEmpty()){
            for(SetmealDish setmealDish :setmealDishes){
                setmealDish.setSetmealId(setmeal.getId());
            }
            setmealDishMapper.deleteBySetmealId(setmeal.getId());
            setmealDishMapper.insertBatch(setmealDishes);
        }


    }

    @Override
    public SetmealVO getById(Long id) {
      Setmeal setmeal =   setmealMapper.getById(id);
      List<SetmealDish> setmealDishes =  setmealDishMapper.getBySetmealId(id);
      SetmealVO setmealVO = new SetmealVO() ;
      BeanUtils.copyProperties(setmeal,setmealVO);
      setmealVO.setSetmealDishes(setmealDishes);
      return setmealVO;
    }

    @Override
    @Transactional
    public void deleteBatch(List<Long> ids) {
        //查询套餐是否在线，在线不允许删除
        for(Long id :ids){
            Setmeal byId = setmealMapper.getById(id);
            if(byId.getStatus()==1){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        //批量删除套餐
        setmealMapper.deleteBatch(ids);
        //删除套餐相关表
        setmealDishMapper.deleteBySetmealIds(ids);
    }

    @Override
    public void updateStatus(Integer status, Long id) {
        setmealMapper.updateStatus(status,id);
    }


    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        return setmealMapper.list(setmeal);
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }
}
